package ru.amse.tsyganov.jumleditor.editor;

import ru.amse.tsyganov.jumleditor.model.ActivityGraph;
import ru.amse.tsyganov.jumleditor.model.State;
import ru.amse.tsyganov.jumleditor.model.Transition;
import ru.amse.tsyganov.jumleditor.view.ActivePoint;
import ru.amse.tsyganov.jumleditor.view.ActivityGraphView;
import ru.amse.tsyganov.jumleditor.view.ModelViewMap;
import ru.amse.tsyganov.jumleditor.view.StateView;
import ru.amse.tsyganov.jumleditor.view.TransitionView;

public class EditorUtil {
	private static final int PRECISION_OF_SELECTION = 10;
	
	private static ActivityGraphView<ActivityGraph> activityGraphView;
	
	public static void initActivityGraphView(
			ActivityGraphView<ActivityGraph> activityGraphView) {
		
		EditorUtil.activityGraphView = activityGraphView;
	}
	
	public static StateView<? extends State> getStateView(int x, int y) {
		
		ActivityGraph graph = activityGraphView.getModel();
		
		for (State s : graph.getPartitions().get(0).getContents()) {
			StateView<? extends State> stateView = 
				ModelViewMap.map.getStatesMap().get(s);
			if (stateView.getRectangle().contains(x, y)) {
				return stateView;
			}
		}
		return null;
	}
	
	public static TransitionView<? extends Transition> getTransitionView(int x, int y) {
		
		ActivityGraph graph = activityGraphView.getModel();
		
		for (State s : graph.getPartitions().get(0).getContents()) {
			for (Transition t : s.getStateVertex().getOutgoing()) {
				State source = t.getSource().getContainer();
				State target = t.getTarget().getContainer();
				
				StateView<? extends State> sourceStateView = 
					ModelViewMap.map.getStatesMap().get(source);
				
				StateView<? extends State> targetStateView = 
					ModelViewMap.map.getStatesMap().get(target);
				
				int x0 = sourceStateView.getCenterX();
				int y0 = sourceStateView.getCenterY();
				
				int x1 = targetStateView.getCenterX();
				int y1 = targetStateView.getCenterY();
				
				double d = Math.abs(((y0 - y1)*x + (x1 - x0)*y + (x0*y1 - x1*y0)) /
						Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2)));
				
				if (x0 > x1) {
					int b = x0;
					x0 = x1;
					x1 = b;
				}
				
				if (y0 > y1) {
					int b = y0;
					y0 = y1;
					y1 = b;
				}
				
				if (x > x0 && x < x1 && y > y0 && y < y1) {
					if (d < PRECISION_OF_SELECTION) {
						return ModelViewMap.map.getTransitionsMap().get(t);
					}
				}
			}
		}
		return null;
	}
	
	public static ActivePoint getActivePoint(int x, int y) {
		
		ActivityGraph graph = activityGraphView.getModel();
		
		for (State s : graph.getPartitions().get(0).getContents()) {
			StateView<? extends State> stateView = 
				ModelViewMap.map.getStatesMap().get(s);
			
			for (ActivePoint p : stateView.getActivePoints()) {
				if (p.getRectangle().contains(x, y)) {
					return p;
				}
			}
			
			for (Transition t : s.getStateVertex().getOutgoing()) {
				TransitionView<? extends Transition> transitionView = 
					ModelViewMap.map.getTransitionsMap().get(t);
				for (ActivePoint p : transitionView.getActivePoints()) {
					if (p.getRectangle().contains(x, y)) {
						return p;
					}
				}
			}
		}
		return null;
	}
}
